package com.java.controller;

import com.java.entity.Blog;
import com.java.entity.PageBean;
import com.java.service.BlogService;
import com.java.utils.PageUtil;
import com.java.utils.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jsoup.nodes.Document;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author wmx
 * 主页controller
 * @create 2020-03-09 0:14
 */
@Component
@RequestMapping("/")
public class IndexController {

    @Resource
    private BlogService blogService;

    /*
     * 请求主页
     */
    @RequestMapping("index")
    public ModelAndView index(@RequestParam(value = "page",required = false)String page,@RequestParam(value = "typeId",required = false)String typeId,@RequestParam(value = "releaseDateStr",required = false)String releaseDateStr,   HttpServletRequest request) throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        StringBuffer param=new StringBuffer();
        if (StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page) ,10);
        Map<String,Object> map = new HashMap<>(16);
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        if (StringUtil.isNotEmpty(typeId)){
            map.put("typeId",typeId);
            param.append(typeId);
        }
        if (StringUtil.isNotEmpty(releaseDateStr)){
            releaseDateStr = StringUtil.formatLike(releaseDateStr.replace("年","-").replace("月","-"));
            map.put("releaseDateStr",releaseDateStr);
            param.append(releaseDateStr);
        }
        List<Blog> blogList = blogService.list(map);
        blogList.stream().forEach(blog -> {
            List<String> imageList =new LinkedList<>();
            String blogInfo = blog.getContent();
            Document document = Jsoup.parse(blogInfo);
             Elements elements = document.select("img[src$=.jpg]");
            for (int i=0;i<elements.size();i++){
                Element element = elements.get(i);
                imageList.add(element.toString());
                if (i==2){
                    break;
                }
            }
            blog.setImageList(imageList);

        });
        modelAndView.addObject("blogList",blogList);

        modelAndView.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index.html",blogService.getTotal(map),Integer.parseInt(page),10,param.toString()));
        modelAndView.addObject("pageTitle","java开源博客系统");
        modelAndView.addObject("mainPage","foreground/blog/list.jsp");
        modelAndView.setViewName("mainTemp");
        return modelAndView;
    }

    /**
     * 源码下载
     */
    @RequestMapping("/downLoad")
    public ModelAndView downLoad() throws Exception{
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageTitle","本站源码下载");
        modelAndView.addObject("mainPage","foreground/down/down.jsp");
        modelAndView.setViewName("mainTemp");
        return modelAndView;
    }

}
